package com.martinwj.camera2demo.utils;

import android.annotation.SuppressLint;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * author: wj
 * created on: 2021/5/31 14:11
 * description:
 */
public class MediaFunc {

    private static String TAG = "MediaFunc";
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
    public static final int MEDIA_TYPE_YUV = 3;
    public static final String GALLERY_PATH = Environment.getExternalStoragePublicDirectory(Environment
            .DIRECTORY_DCIM) + File.separator;
    public static final String SAVE_PATH = "Camera2Demo";

    public static File getOutputMediaFile(int type, String tag) {
//        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
//                Environment.DIRECTORY_DCIM), SAVE_PATH);
        File mediaStorageDir = new File(GALLERY_PATH, SAVE_PATH);
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.e(TAG, "failed to create directory");
                return null;
            }
        }

        @SuppressLint("SimpleDateFormat")
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath()
                    + File.separator + "IMG_" + tag + "_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath()
                    + File.separator + "VID_" + tag + "_" + timeStamp + ".mp4");
        } else if (type == MEDIA_TYPE_YUV) {
            mediaFile = new File(mediaStorageDir.getPath()
                    + File.separator + "IMG_" + tag + "_" + timeStamp + ".yuv");
        }else {
            return null;
        }
        return mediaFile;
    }


}
